package com.hsogoo.avatar.spring;

import java.net.InetSocketAddress;
import java.net.SocketAddress;

import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import com.hsogoo.avatar.enums.Protocol;
import com.hsogoo.avatar.processor.DefaultAvatarProcessor;
import com.hsogoo.avatar.transport.netty.AvatarNettyAcceptor;

/**
 * @Author huangsaigang
 * Created @2018/9/29.
 */
//@Component
public class AvatarServerInitializer implements ApplicationListener<ContextRefreshedEvent> {

    private final String host = "localhost";
    private final int port = 8848;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        SocketAddress address = new InetSocketAddress(host, port);
        AvatarNettyAcceptor acceptor = new AvatarNettyAcceptor(Protocol.AVATAR, address);
        acceptor.withProcessor(new DefaultAvatarProcessor());
        try {
            acceptor.start();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}
